<?php

namespace Boosen\Company\app\Console\Commands;

use Illuminate\Console\Command;
use Log;

use Boosen\Company\app\Models\CNBaseUrl;
use Boosen\Company\app\Models\CNCompanyUrl;

use Goutte\Client;
use GuzzleHttp\Client as GuzzleClient;

class CNCompanyUrlSpider extends Command
{
    const PAGE_SIZE = 2;
    
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cn:company_url {page?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $page = $this->argument('page') ?: 1;
        while (true) {
            $pageObj = CNBaseUrl::simplePaginate(self::PAGE_SIZE, ['*'], 'page', $page);
            $break = false;
            foreach ($pageObj->items() as $baseUrl) {
                $break = !$this->processUrl($baseUrl->url);
                if ($break) break;
                sleep(5);
            }
            if ($break) {
                Log::channel('cn_company')->error('CN base url stop when process page ' . $page);
                break;
            }
            $page++;
        }
        
        // print_r($pageObj->items()[0]);exit;
    }

    private function processUrl($url)
    {
        try {
            $goutteClient = new Client();
            $guzzleClient = new GuzzleClient(array(
                'timeout' => 60,
            ));
            $goutteClient->setClient($guzzleClient);

            $oUrl = $url;
            $nbCompany = 0;
            $page = 1;
            while (true) {
                $url =  $oUrl . '?p=' . $page;
                $crawler = $goutteClient->request('GET', $url);
                $links = $crawler->filterXPath('//ul[@class="extension_ul"]/li/div[@class="extension_right"]/p/a')->links();
                if (count($links) == 0) break;
                foreach ($links as $link) {
                    // $host = parse_url($link->getUri(), PHP_URL_HOST);
                    $shortName = explode('.', parse_url($link->getUri(), PHP_URL_HOST))[0];
                    CNCompanyUrl::updateOrCreate(
                        ['short_name' => $shortName], 
                        ['url' => $link->getUri()]
                    );
                    $nbCompany++;
                    $this->info("Update company " . $link->getNode()->nodeValue);
                }

                $this->info($nbCompany . 'companys updated');
                if ($page%2 == 0) {
                    sleep(5);
                } 
                $page++;

            }
            return true;

            // print_r($crawler->filterXPath('//ul[@class="extension_ul"]/li')->html());exit;
        } catch (\Exception $e) {
            $this->info($e->getMessage());
            Log::channel('cn_company')->error($e->getMessage());
            return false;
        }
    }
}
